#!/usr/bin/perl -w
use strict;

$#ARGV == 1 or die("incorrect usage!\nUsage:\n./scan_pileup.pl <pileup-file> <report-file>\n");

my $fh;

open $fh,$ARGV[1] or die "can not open file at $!";
my @report;
my %rep_hash;
while(<$fh>){
	chomp;
	#$_ =~ s/(\s+)/(\t)/g;	
	#push @report,$_;
	$_ =~ /(\S+)(\s+)(\d+)(\s+)([ATGCN-])(\s+)(.*)/;
	$rep_hash{$1."\t".$3."\t".$5} = $7;
}
close $fh;

my @scan_pileup;
my %depth_of_cov; 
my %pileup_hash;
open $fh,$ARGV[0] or die "can not open file at $!";
while(<$fh>){
	chomp;
##################################################
#chr22   16049998        N       4       ^:,^:,^:,^:,    2222
##################################################
	$_ =~ /(.*?)([ATGCN])(\s+)(\d+)(\s+)(\S+)(\s+)(\S+)/i;
	#$_ =~ /(\S+)(\s+)(\d+)([ATGCN])(\s+)(\d+)(\s+)(\S+)(\s+)(\d+)/;
	my $header = $1;
	my $num_reads = $4;
	my $read_rep = $6;
	my $chr_label = $2; 

#	print STDERR "***\n$_\n\$1 $1\n\$2 $2\n\$3 $3\n\$4 $4\n\$5 $5\n\$6 $6\n***\n";
	$header =~ /(\S+)(\s+)(\d+)(\s+)/;
	$header = $1."\t".$3."\t";
	$depth_of_cov{$1."\t".$3} = $num_reads;
	my $chr_name = $1;
	my $chr_pos = $3;

#	<STDIN>;
	$read_rep =~ s/\$//g;
	$read_rep =~ s/\^(.)//g;
	$read_rep =~ s/[\<\>]//g;

	my $match_count = 0;
	my $max = 0;
##################################################
##	, and .
##################################################
	while($read_rep =~ /[\.\,]/){
		$match_count++;
		$read_rep =~ s/[\,\.]//;
	}
	
	$max = $match_count;
	my $majority = "match";

	#print "\nmatch: $match_count ";

	my $del_count = 0;
##################################################
##	*
##################################################
	while($read_rep =~ /[\*]/){
		$del_count++;
		$read_rep =~ s/[\*]//;
	}

	if($del_count > $max){
		$max = $del_count;
		$majority="delete";
	}
	#print "deleted: $del_count ";

	my %hash;
##################################################
##    [+-][0-9]+[ATGCN]+
##################################################
	while($read_rep =~ /(.*)([\+\-])([0-9]+)(.*)/){
		my $beg_of_read_rep = $1;
		my $indel_ind = $2;
		my $indel_len = $3;
		my $rest_of_read_rep = $4;
		
		my $indel_str = substr $rest_of_read_rep, 0, $indel_len;
		$indel_str = lc($indel_str);

		$hash{$indel_ind.$indel_str} += $indel_len;
		$read_rep = $beg_of_read_rep.substr $rest_of_read_rep,$indel_len;
	}
##################################################
##     rest of ATGCN|atgcn
##################################################
	while($read_rep =~ /([ATGCN])(.*)/i){
		$hash{lc($1)}++;
		$read_rep =~ s/([ATGCN])(.*)/$2/i;
	}

	foreach my $key (keys %hash){
		# print "$key=>$hash{$key} ";
		if($hash{$key} > $max){
			$max = $hash{$key};
			$majority = $key;
		} 
	}
	
	if($majority eq "match"){
#		print "match($max) at $chr_pos\n";
	}elsif($majority eq "delete"){
#		print "delete($max) at $chr_pos\n";
#		<STDIN>;
#		print $header.$chr_label."\t-\n";
		push @scan_pileup,$header.$chr_label."\t-";
		$pileup_hash{$header.$chr_label} = "-";
#		<STDIN>; 
	}else{
#		print "indel($max)($majority) at $chr_pos\n";
#		<STDIN>;
		if($majority =~ /([\+\-])(.*)/){
#			print $header.$1."\t".uc($2)."\n";
			push @scan_pileup,$header.$1."\t".uc($2); 
#			<STDIN>;
			$pileup_hash{$header.$1} = uc($2);
 
		}else{
#			print $header.$chr_label."\t".uc($majority)."\n";
			push @scan_pileup,$header.$chr_label."\t".uc($majority); 
			$pileup_hash{$header.$chr_label} = uc($majority);
#			<STDIN>; 
		}
	}
}
close $fh;

foreach my $key (keys %pileup_hash){
#	print "$key=>$pileup_hash{$key}\n";
	if(exists $rep_hash{$key}){
		print "report and pileup match at\t\t$key\t$rep_hash{$key}\n";	
	}else{
		$key =~ /(\S+)(\s+)(\S+)(\s+)/;
		print "false positive at\t\t\t$key\t$pileup_hash{$key},\tdepth of coverage\t$depth_of_cov{$1.$2.$3}\n";
	}
}

print "##########\n";
foreach my $key (keys %rep_hash){
#	print "$key=>$rep_hash{$key}\n";
	if(!exists $pileup_hash{$key}){
		$key =~ /(\S+)(\s+)(\S+)(\s+)/;
		print "false negative at\t\t\t$key\t$rep_hash{$key},\tdepth of coverage\t$depth_of_cov{$1.$2.$3}\n";	
	}
}

